package com.rawchen.controller.admin;

import com.github.pagehelper.PageInfo;
import com.rawchen.entity.ExceptionLog;
import com.rawchen.model.vo.Result;
import com.rawchen.service.ExceptionLogService;
import com.rawchen.util.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: 异常日志后台管理
 */
@RestController
@RequestMapping("/admin")
public class ExceptionLogController {
	@Autowired
	private ExceptionLogService exceptionLogService;

	/**
	 * 分页查询异常日志列表
	 *
	 * @param date     按操作时间查询
	 * @param pageNum  页码
	 * @param pageSize 每页个数
	 */
	@GetMapping("/exceptionLogs")
	public Result exceptionLogs(@RequestParam(defaultValue = "") String[] date,
	                            @RequestParam(defaultValue = "1") Integer pageNum,
	                            @RequestParam(defaultValue = "10") Integer pageSize) {
		if (date.length != 0 && date.length != 2)
			return Result.create(HttpStatus.BAD_REQUEST.value(), "请求参数有误，date必须为两个参数");
		date = WebUtil.pageProcess(date, pageNum, pageSize);
		PageInfo<ExceptionLog> pageInfo = new PageInfo<>(exceptionLogService.getExceptionLogListByDate(date[0], date[1]));
		return Result.ok("请求成功", pageInfo);
	}

	/**
	 * 按id删除异常日志
	 *
	 * @param id 日志id
	 */
	@DeleteMapping("/exceptionLog")
	public Result delete(@RequestParam Long id) {
		exceptionLogService.deleteExceptionLogById(id);
		return Result.ok("删除成功");
	}
}
